﻿Imports BusinessLayer
Imports CommonLayer
Imports Infragistics.Win.UltraWinToolbars

Public Class frmMain

#Region "Declaration"

    Private skinProcessing As Boolean = False
    Private currentPanelSize As Integer
    Private isCollapse As Boolean = False
    Public mscMDIChildToolName As String = "ChildTool"

#End Region

#Region "Property"

#End Region

#Region "Constructor"

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        SetEnableButtonOnToolbar(False)
    End Sub

#End Region

#Region "Method"

    ''' <summary>
    ''' '
    ''' </summary>
    ''' <param name="frmChild"></param>
    ''' <param name="blnModal"></param>
    ''' <remarks></remarks>
    Public Sub ShowMDIClient(ByVal frmChild As Form, Optional ByVal blnModal As Boolean = False) _
        ', Optional ByVal eRefType As EnumRefType = EnumRefType.NonVoucher)
        Me.Cursor = Cursors.WaitCursor
        Try
            If blnModal Then
                With frmChild
                    .Owner = Me
                    .ShowInTaskbar = False
                    .StartPosition = FormStartPosition.CenterParent
                    .ShowDialog()
                    .Dispose()
                End With
            Else
                With frmChild
                    .MinimizeBox = False
                    .MaximizeBox = False
                    .ControlBox = False
                    .MdiParent = Me
                    .Show()
                    .WindowState = FormWindowState.Maximized
                    .BringToFront()
                End With
                CloseAllForms(frmChild)
            End If
        Catch ex As Exception
            Throw ex
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Public Sub SetLogInInfo(ByVal isClear As Boolean, ByVal strDB As String, ByVal strServer As String, _
                            ByVal strUser As String)
        If isClear Then
            StatusMain.Panels("lblDatabase").Text = "CSDL:"
            StatusMain.Panels("lblDatabase").ToolTipText = "Cơ sở dữ liệu:"
            StatusMain.Panels("lblServer").Text = "Máy chủ:"
            StatusMain.Panels("lblUser").Text = "Người dùng:"
        Else
            StatusMain.Panels("lblDatabase").Text = "CSDL: " & strDB
            StatusMain.Panels("lblDatabase").ToolTipText = "Cơ sở dữ liệu: " & strDB
            StatusMain.Panels("lblServer").Text = "Máy chủ: " & strServer
            StatusMain.Panels("lblUser").Text = "Người dùng: " & strUser
        End If
    End Sub

    Public Sub SetMainStatusMessage(ByVal strMess As String)
        If Not String.IsNullOrEmpty(strMess) Then
            StatusMain.Panels("lblSpring").Visible = True
            StatusMain.Panels("lblOther").Text = strMess
            StatusMain.Panels("lblOther").Visible = True
        Else
            StatusMain.Panels("lblOther").Visible = False
            StatusMain.Panels("lblSpring").Visible = False
            StatusMain.Panels("lblOther").Text = String.Empty
        End If
    End Sub

    Public Sub SetEnableButtonOnToolbar(ByVal bEnable As Boolean)
        'Dim oItemVisible As BarItemVisibility = IIf(bEnable, BarItemVisibility.Always, BarItemVisibility.Never)
        'popBusiness.Visibility = oItemVisible
        'popDictionary.Visibility = oItemVisible
        'popSystem.Visibility = oItemVisible
        'mnuCloseDB.Enabled = bEnable
        'mnuOpenDB.Enabled = Not bEnable
        'mnuDBInfo.Enabled = bEnable
        'ToolPaintStyle.Visible = bEnable
        'ToolMain.Visible = bEnable
        ''bMDIChild.Visible = bEnable
        'mnuSearch.Enabled = bEnable
        'mnuReport.Enabled = bEnable
        'mnuCreateDB.Enabled = Not bEnable
        'splSideBar.Visible = bEnable
        'pnlSideBar.Visible = bEnable
        'NavBarControlMain.Visible = bEnable
    End Sub

    ''' <summary>
    ''' </summary>
    ''' <param name="frmCurent"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function CloseAllForms(ByVal frmCurent As Form) As Boolean
        Dim blnResult As Boolean = True
        Try
            Dim frmChild As Form
            For Each frmChild In Me.MdiChildren
                If frmCurent IsNot Nothing Then
                    If frmChild.GetType.ToString <> frmCurent.GetType.ToString Then
                        frmChild.Close()
                    End If
                Else
                    frmChild.Close()
                End If
            Next
        Catch ex As Exception
            blnResult = False
        End Try
        Return blnResult
    End Function

    Public Function Login() As Boolean
        Dim bResult As Boolean = False
        Dim frm As New frmLogIn
        With frm
            .StartPosition = FormStartPosition.CenterParent
            .WindowState = FormWindowState.Normal
            .ShowInTaskbar = False
            If .ShowDialog = DialogResult.OK Then
                bResult = True
                Dim oBLDictionary As New BLDictionary
                Dim dsSYS As New DataSetSystem
                With oBLDictionary
                    .BusinessObject = dsSYS
                    .TableName = dsSYS.AppOption.TableName
                    .GetAllData()
                End With
                CommonFunction.MySession = New ApplicationSession(dsSYS.AppOption)

                Me.SetLogInInfo(False, GetSetting(Application.ProductName, "DatabaseName", "DatabaseName"), _
                                GetSetting(Application.ProductName, "ServerName", "ServerName"), .UserName)

                CommonFunction.MySession.CurrentDatabaseName = GetSetting(Application.ProductName, "DatabaseName", _
                                                                          "DatabaseName")
                CommonFunction.MySession.CurrentSQLInstanceName = GetSetting(Application.ProductName, "ServerName", _
                                                                             "ServerName")
                CommonFunction.MySession.CurrentUser = .UserName
                CommonFunction.MySession.IsLogIn = True
            End If
        End With
        Return bResult
    End Function

    Public Sub LogOut()
        CloseAllForms(Me)
        SetEnableButtonOnToolbar(False)
        CommonFunction.MySession.IsLogIn = False
        Me.SetLogInInfo(True, Nothing, Nothing, Nothing)
    End Sub

#End Region

#Region "Events"

    Private Sub ToolbarsManager_ToolClick(ByVal sender As Object, ByVal e As ToolClickEventArgs) _
        Handles ToolbarsManager.ToolClick
        Try
            ToolEventHandler.ToolBarHandler(Me, e.Tool)
        Catch ex As Exception
            CommonFunction.ShowExclamation(ex.ToString)
        End Try
    End Sub

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Try
            If CommonFunction.mscBuildApplication AndAlso CommonFunction.mscDemoBuildApplication Then
                If Not clsEncryptString.SaveFirstDate(My.Application.Info.ProductName) Then
                    clsEncryptString.SaveLastDate(My.Application.Info.ProductName)
                    Dim iResult As Integer = clsEncryptString.CheckDayExpired(My.Application.Info.ProductName)
                    Select Case iResult
                        Case - 2
                            CommonFunction.ShowExclamation( _
                                "Do thiết lập ngày hệ thống không đúng nên bạn không được phép sử dụng chương trình!")
                            Me.Close()
                        Case - 1
                            CommonFunction.ShowExclamation("Bạn đã hết hạn dùng thử! Vui lòng đăng ký GPSD khác.")
                            Me.Close()
                    End Select
                End If
            End If

            If Login() Then
                SetEnableButtonOnToolbar(True)
            Else
                CloseAllForms(Me)
                SetEnableButtonOnToolbar(False)

#If DEBUG Then
                CommonFunction.MySession = New ApplicationSession()
#End If

            End If
        Catch ex As Exception
            CommonFunction.ShowExclamation(ex.ToString)
        End Try
    End Sub

#End Region
End Class
